﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;

/// <summary>
/// Yun_Img 的摘要说明
/// </summary>
public class Yun_Img
{
    public Yun_Img()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="pageSize">每页数</param>
    /// <param name="pageIndex">当前第多少页</param>
    /// <param name="count">数据总数</param>
    /// <param name="where">查询条件</param>
    /// <returns></returns>
    public ArrayList GetList(int pageSize, int pageIndex, out int count, string where)
    {
        string sqlcount = "select count(0) from Yun_Img where 1=1 " + where;
        string sql = string.Format("select top {0} id,imgClass,imgName,imgSuffix,imgSaveName,createdate from Yun_Img where 1 = 1 {2} and ID not in(select top {1} id from Yun_Img where 1=1 " + where + " order by createdate desc)order by createdate desc;"
              , pageSize, (pageIndex - 1) * pageSize, where);
        ArrayList data = new SQLHelper("getnum").Select(sql);
        count = Convert.ToInt32(new SQLHelper("getnum").ExecuteScalar(sqlcount));
        return data;
    }

    /// <summary> 
    /// 通过id得到Hashtable实体
    /// </summary> 
    /// <param name="id">id</param>
    /// <returns>返回实体对象</returns>
    public Hashtable GetModel(string id)
    {
        string sql = "select id,imgClass,imgName,imgSuffix,imgSaveName,createdate from Yun_Img where id = " + id;
        ArrayList data = new SQLHelper("getnum").Select(sql);
        return data.Count > 0 ? (Hashtable)data[0] : null;
    }

    /// <summary>
    /// 传入查询参数实体条件 获取model
    /// </summary>
    /// <param name="n"></param>
    /// <returns></returns>
    public Hashtable GetModel(Hashtable n)
    {
        string sql = "select * from Yun_Img where 1=1 ";
        foreach (string key in n.Keys)
        {
            sql += string.Format(" and {0}=@{0} ", key);
        }
        ArrayList data = new SQLHelper("getnum").Select(sql, n);
        return data.Count > 0 ? (Hashtable)data[0] : null;
    }
    /// <summary>
    /// 传入查询参数实体条件 获取DataTable
    /// </summary>
    /// <param name="n"></param>
    /// <returns></returns>
    public DataTable GetModel_dt(Hashtable n)
    {
        string sql = "select * from Yun_Img where 1=1 ";
        foreach (string key in n.Keys)
        {
            sql += string.Format(" and {0}=@{0} ", key);
        }
        DataTable data = new SQLHelper("getnum").ExcuteQuery(sql);
        return data;
    }

    /// <summary>
    /// 检测是否存在 返回存在数量
    /// </summary>
    /// <param name="imgName">imgName</param>
    /// <returns></returns>
    public int Getcunzai(string imgName)
    {
        string sql = string.Format("select count(0) from Yun_Img where 1=1 and imgSaveName = '{0}'", imgName);
        int count = Convert.ToInt32(new SQLHelper("getnum").ExecuteScalar(sql));
        return count;
    }

    /// <summary>
    /// 账号获取ID
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public static object GetUserID(string num)
    {
        string sql = "select ID from Yun_Img where number = '" + num + "'";
        object data = new SQLHelper("getnum").ExecuteScalar(sql);
        return data;
    }
    /// <summary> 
    /// 通过账号得到Hashtable实体
    /// </summary> 
    /// <param name="biaohao">biaohao</param>
    /// <returns>返回实体对象</returns>
    public Hashtable GetModel_bh(string num)
    {
        string sql = "select * from Yun_Img where number = '" + num + "'";
        ArrayList data = new SQLHelper("getnum").Select(sql);
        return data.Count > 0 ? (Hashtable)data[0] : null;
    }
    public Hashtable GetModel_bh(string num, string numpower)
    {
        string sql = "select * from Yun_Img where number = '" + num + "' and numpower = '" + numpower + "'";
        ArrayList data = new SQLHelper("getnum").Select(sql);
        return data.Count > 0 ? (Hashtable)data[0] : null;
    }

    /// <summary>
    /// 修改全体公告
    /// </summary>
    /// <param name="Notice"></param>
    /// <returns></returns>
    public int Upd_All_Notice(string Notice)
    {
        string sql = "update Yun_Img set Notice = '" + Notice + "'";
        return new SQLHelper("getnum").Execute(sql);
    }

    /// <summary> 
    /// 更新数据
    /// </summary> 
    /// <param name="n">Hashtable</param>
    /// <returns>返回受影响行数</returns>
    public int Update(Hashtable n)
    {
        string updateColumns = string.Empty;
        foreach (string key in n.Keys)
        {
            if (key == "id")
                continue;
            if (updateColumns != string.Empty)
                updateColumns += ", ";
            updateColumns += string.Format("{0}=@{0}", key, key);
        }
        string sql = string.Format("update Yun_Img set {0} where id=@id", updateColumns);
        return new SQLHelper("getnum").Execute(sql, n);
    }
    public int Update_imgName(Hashtable n)
    {
        string updateColumns = string.Empty;
        foreach (string key in n.Keys)
        {
            if (key == "imgName")
                continue;
            if (updateColumns != string.Empty)
                updateColumns += ", ";
            updateColumns += string.Format("{0}=@{0}", key, key);
        }
        string sql = string.Format("update Yun_Img set {0} where imgName=@imgName", updateColumns);
        return new SQLHelper("getnum").Execute(sql, n);
    }
    /// <summary>
    /// 新增数据
    /// </summary>
    /// <param name="n"></param>
    /// <returns></returns>
    public int insert(Hashtable n)
    {
        string insertColumns = string.Empty;
        string insertParams = string.Empty;
        foreach (string key in n.Keys)
        {
            if (key == "chongzhi")
            {
                continue;
            }
            if (insertColumns != string.Empty)
            {
                insertColumns += ", ";
                insertParams += ", ";
            }
            insertColumns += string.Format("{0}", key);
            insertParams += string.Format("@{0}", key);
        }
        string sql = string.Format("insert into Yun_Img ({0}) values ({1}) ;", insertColumns, insertParams);
        return new SQLHelper("getnum").Execute(sql, n);
    }



}